package com.cl.code.web.component.exception;

import com.cl.code.web.common.model.ApiResult;
import com.cl.code.web.common.model.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @author chengliang
 * @since 1.0.0
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理所有未被捕获的异常
     *
     * @param e 异常对象
     * @return ApiResult 封装的错误响应
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ApiResult<Void> handleException(Exception e) {
        return handleException(e, ResultEnum.INTERNAL_SERVER_ERROR);
    }

    /**
     * 处理非法参数异常
     *
     * @param e 异常对象
     * @return ApiResult 封装的错误响应
     */
    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ApiResult<Void> handleIllegalArgumentException(IllegalArgumentException e) {
        return handleException(e, ResultEnum.BAD_REQUEST);
    }

    /**
     * 处理空指针异常
     *
     * @param e 异常对象
     * @return ApiResult 封装的错误响应
     */
    @ExceptionHandler(NullPointerException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ApiResult<Void> handleNullPointerException(NullPointerException e) {
        return handleException(e, HttpStatus.INTERNAL_SERVER_ERROR, "空指针异常，请检查代码逻辑");
    }

    @ExceptionHandler(IOException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ApiResult<Void> handleIOException(IOException e) {
        return handleException(e, HttpStatus.INTERNAL_SERVER_ERROR, "IO异常，请检查文件或网络连接");
    }


    public ApiResult<Void> handleException(Exception e, ResultEnum resultEnum) {
        String message = e.getMessage() != null ? e.getMessage() : resultEnum.getMessage();
        printStackTrace(e, message);
        return ApiResult.error(resultEnum.getCode(), message);
    }

    public ApiResult<Void> handleException(Exception e, HttpStatus httpStatus, String message) {
        message = e.getMessage() != null ? e.getMessage() : message;
        printStackTrace(e, message);
        return ApiResult.error(httpStatus.value(), message);
    }

    public ApiResult<Void> handleException(Exception e, int code, String message) {
        message = e.getMessage() != null ? e.getMessage() : message;
        printStackTrace(e, message);
        return ApiResult.error(code, message);
    }

    public ApiResult<Void> handleException(Exception e, String message, ResultEnum resultEnum) {
        printStackTrace(e, message);
        return ApiResult.error(resultEnum.getCode(), message);
    }

    public void printStackTrace(Throwable e, String message) {
        StringWriter stringWriter = new StringWriter();
        PrintWriter writer = new PrintWriter(stringWriter);
        e.printStackTrace(writer);
        StringBuffer buffer = stringWriter.getBuffer();
        Logger log = LoggerFactory.getLogger(e.getClass());
        log.error("{}\n{}", message, buffer.toString());
    }

}
